﻿// Copyright (c) 2012 - 2020 Jared Zheng <jaredz at outlook dot com>
// Buddha Framework is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Create : 2012-12-01 version 0.1 Jared Zheng <jaredz at outlook dot com>
// Update : 2018-12-05 version 3.1.5 Jared Zheng <jaredz at outlook dot com>

#include "stdafx.h"
#include "resource.h"
#include "ServerLoader.h"
#include "MasterDBDlg.h"

///////////////////////////////////////////////////////////////////////////////////////////////////////////
// CMasterDBDlg
XBEGIN_DLG_ID_WND_EXCHANGE( CMasterDBDlg )
XDLG_ID_WND( IDC_CONNECT_CENTER,  m_ConnectCenter )
XDLG_ID_WND( IDC_LISTEN_INTERNAL, m_ListenInternal )
XDLG_ID_WND( IDC_MASTERDB_INFO,   m_ServerInfo )
XDLG_ID_WND( IDD_MASTER,          m_MasterSvr )
XDLG_ID_WND( IDD_SERVICE,         m_ServiceSvr )
XEND_DLG_ID_WND_EXCHANGE( CXDlg )

CMasterDBDlg::CMasterDBDlg(void)
: m_uMasterDBServer(0)
, m_uMasterDBQuery(0)
, m_uMasterDBLoad(0)
, m_uMasterCount(0)
, m_uMasterGate(0)
, m_uMasterService(0)
, m_uServiceCount(0)
, m_uServiceOnline(0)
, m_uServiceTotal(0)
{
}

CMasterDBDlg::~CMasterDBDlg(void)
{
}

LRESULT CMasterDBDlg::OnInitDialog(void)
{
    RECT rc;
    GetClientRect(&rc);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);

    Long lHeight = (rc.bottom - rc.top) / 2;
    m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
    rc.top += (DLG_ITEM_GRAP + lHeight);
    m_ServiceSvr.MoveWindow(0, rc.top, rc.right, lHeight);
    //
    CStringFix strTemp;
    strTemp.Load(IDC_CONNECT_CENTER);
    m_ConnectCenter.SetWindowText(*strTemp);

    strTemp.Load(IDC_LISTEN_INTERNAL);
    m_ListenInternal.SetWindowText(*strTemp);
    //
    InitServerData(PAKT_MASTER);
    InitServerData(PAKT_SERVICE);
    return TRUE;
}

LRESULT CMasterDBDlg::OnSize(WPARAM, LPARAM lParam, BOOL&)
{
    RECT rc   = { 0 };
    rc.right  = (Long)LOWORD(lParam);
    rc.bottom = (Long)HIWORD(lParam);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    m_ServerInfo.MoveWindow(0, rc.top, rc.right, DLG_ITEM_HEIGHT);

    rc.top += (DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_GRAP + DLG_ITEM_HEIGHT);
    Long lHeight = (rc.bottom - rc.top) / 2;
    m_MasterSvr.MoveWindow(0, rc.top, rc.right, lHeight);
    rc.top += (DLG_ITEM_GRAP + lHeight);
    m_ServiceSvr.MoveWindow(0, rc.top, rc.right, lHeight);

    rc.right /=2;
    if (rc.right > DLG_ITEM_WIDTH)
    {
        m_ConnectCenter.MoveWindow(0, 0, (rc.right - DLG_ITEM_GRAP), (DLG_ITEM_GRAP + DLG_ITEM_HEIGHT));
        m_ListenInternal.MoveWindow((rc.right + DLG_ITEM_GRAP), 0, (rc.right - DLG_ITEM_GRAP), (DLG_ITEM_GRAP + DLG_ITEM_HEIGHT));
    }
    return 0;
}

void CMasterDBDlg::OnLive(bool bStart)
{
    CStringFix strTemp;
    strTemp.Load(IDC_LISTEN_INTERNAL);
    if (bStart)
    {
        CStrAddr strAddr;
        GServerLoaderInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_MASTERDB, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);
        m_ListenInternal.SetWindowText(*strTemp);
    }
    else
    {
        m_ListenInternal.SetWindowText(*strTemp);

        strTemp.Load(IDC_CONNECT_CENTER);
        m_ConnectCenter.SetWindowText(*strTemp);

        m_uMasterDBServer = 0;
        m_uMasterDBQuery  = 0;
        m_uMasterDBLoad   = 0;
        m_uMasterCount    = 0;
        m_uMasterGate     = 0;
        m_uMasterService  = 0;
        m_uServiceCount   = 0;
        m_uServiceOnline  = 0;
        m_uServiceTotal   = 0;
    }
    m_ServerInfo.SetWindowText(TF(""));
}

void CMasterDBDlg::OnLink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        CStringFix strTemp;
        strTemp.Load(IDC_CONNECT_CENTER);

        CNetAddr* pAddr = (reinterpret_cast<CNetAddr*>(utData));

        CStrAddr   strAddr;
        GServerLoaderInst->m_Config.GetServerAddr(CServerConfig::CFG_LOAD_CENTER, CServerConfig::CFG_LOAD_NONE, strAddr);
        strTemp.AppendFormat(TF("[%s]:%d"), *strAddr.strIp, strAddr.usPort);

        GServerLoaderInst->m_NetworkPtr->TranslateAddr(strAddr.strIp, strAddr.usPort, *pAddr, false);
        strTemp.AppendFormat(TF("([%s]:%d)"), *strAddr.strIp, strAddr.usPort);

        m_ConnectCenter.SetWindowText(*strTemp);
    }
    else
    {
        AddServerData(nServerIndex, utData);
    }
}

void CMasterDBDlg::OnUpdate(Int nServerIndex, uintptr_t utData)
{
    UpdateServerData(nServerIndex, utData);
}

void CMasterDBDlg::OnSync(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_SERVICE)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uServiceTotal  = pStatus->uTotal;
        m_uServiceOnline = pStatus->uOnline;
        m_uServiceCount  = pStatus->usLoad;
    }
    else if (nServerIndex == PAKT_MASTER)
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        m_uMasterGate    = pStatus->uTotal;
        m_uMasterService = pStatus->uOnline;
        m_uMasterCount   = pStatus->usLoad;
    }
    else
    {
        PSERVER_STATUS pStatus = reinterpret_cast<PSERVER_STATUS>(utData);
        assert(nServerIndex == PAKT_MASTERDB);
        m_uMasterDBServer = pStatus->uTotal;
        m_uMasterDBQuery  = pStatus->uOnline;
        m_uMasterDBLoad   = pStatus->usLoad / DATAD_TOPERCENT;
    }
}

void CMasterDBDlg::OnSync(Int, CStream&)
{
}

void CMasterDBDlg::OnUnlink(Int nServerIndex, uintptr_t utData)
{
    if (nServerIndex == PAKT_CENTER)
    {
        CStringFix strTemp;
        strTemp.Load(IDC_CONNECT_CENTER);
        m_ConnectCenter.SetWindowText(*strTemp);
    }
    else
    {
        RemoveServerData(nServerIndex, utData);
    }
}

void CMasterDBDlg::UpdateServerInfo(void)
{
    CStringFix strInfo;
    strInfo.Load(IDC_MASTERDB_INFO);

    CStringFix strTemp;
    strTemp.Format(*strInfo, m_uMasterDBServer, m_uMasterDBQuery, m_uMasterDBLoad,
                   m_uMasterCount, m_uMasterGate, m_uMasterService,
                   m_uServiceCount, m_uServiceOnline, m_uServiceTotal);
    m_ServerInfo.SetWindowText(*strTemp);
}

CXListView* CMasterDBDlg::GetListView(Int nServerIndex)
{
    if (nServerIndex == PAKT_MASTER)
    {
        return &m_MasterSvr;
    }
    else
    {
        assert(nServerIndex == PAKT_SERVICE);
        return &m_ServiceSvr;
    }
}





